package controller;

import po.College;
import po.Manager;
import service.CollegeService;
import service.ManagerService;
import service.serviceImpl.CollegeServiceImpl;
import service.serviceImpl.ManagerServiceImpl;
import util.ServletRepeat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 润
 */
@WebServlet(name = "ManagerServlet",urlPatterns = "/ManagerServlet")
public class ManagerServlet extends BaseServlet  {
      private ManagerService service= new ManagerServiceImpl();
      private CollegeService collegeService=new CollegeServiceImpl();
    public void login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Manager manager = service.queryStudentByNameAndPassword(username, password);
        if(manager!=null){
            HttpSession session = request.getSession();
            session.setAttribute("manager",manager);
            boolean result1=true;
            String json1 = ServletRepeat.getJson(result1);
            response.getWriter().write(json1);
        }else{
            boolean result2=true;
            String json2 = ServletRepeat.getJson2(result2);
            response.getWriter().write(json2);
        }

    }
    public void queryAllCollege(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<College> colleges = collegeService.queryAllCollege();
        if(colleges.size()>0){
            request.setAttribute("colleges",colleges);
            request.getRequestDispatcher("/allCollege.jsp").forward(request,response);

        }else{
            request.setAttribute("img","暂时无学院");
            request.getRequestDispatcher("/emptyCollege.jsp").forward(request,response);
        }
    }
    public void addCollege(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //执行service层代码
        String name = request.getParameter("name");
        College college = new College();
        college.setName(name);
        collegeService.insert(college);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);

    }
    public void deleteCollege(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        College college = collegeService.queryOne(cid);
        collegeService.delete(college);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }

    public void queryOneCollege(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        College college = collegeService.queryOne(cid);
        request.setAttribute("college",college);
        request.getRequestDispatcher("/updateCollege.jsp").forward(request,response);

    }

    public void updateCollege(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        College college = new College();
        college.setId(cid);
        college.setName(name);
        collegeService.update(college);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }

}
